package com.example;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient.EurekaServiceInstance;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.discovery.converters.Auto;

/**
 * Created by libin on 03/03/2017.
 */

@RestController
public class ApplicationController {

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/")
    public String getIndex(){
        String message = restTemplate.getForObject("http://bookservice/",String.class);
        return "Web+" + message;
    }
    
    @Autowired
    DiscoveryClient dc;
    
    @RequestMapping("/a")
    public String getA(){
    	List<ServiceInstance> sis = dc.getInstances("bookservice");
    	String allC = "bookservice:";
    	for(ServiceInstance si : sis) {
			EurekaServiceInstance esi = (EurekaServiceInstance)si;
			allC = allC + "["+si.getHost() + ":" + esi.getInstanceInfo().getIPAddr() + ":" + si.getPort()+"]";
		}
    	
    	List<ServiceInstance> sis2 = dc.getInstances("web");
    	allC = "|||||||web:"+allC;
    	for(ServiceInstance si : sis2) {
			EurekaServiceInstance esi = (EurekaServiceInstance)si;
			allC = allC + "["+si.getHost() + ":" + esi.getInstanceInfo().getIPAddr() + ":" + si.getPort()+"]";
		}
    	return allC;
    }
}
